<?
include "connectDB.php";


//echo mysql_num_rows($result1);
$query2 = 'select concept from relation';
$result2 = mysql_query($query2) or die(mysql_error());
$total_concepts = mysql_num_rows($result2);
echo $total_concepts;
$m=0;
$c=0;
$concepts;
$matched;
$topic;
$g=0;
	while($categories = mysql_fetch_array($result2)){
		$concept = strtolower(str_replace('_',' ',$categories['concept']));
		//echo $concept.'<br>';
		//if(trim($post) != null){
		//if(substr_count($post, $concept)) $matches++;;
		//echo substr_count($post, 'linux');
		//echo $concept.'\n';
		$concepts[$g++]=$concept;
		$c++;	
	}
	
		$v=0;
	$query1 = 'select * from so_posts';
	$result1 = mysql_query($query1) or die(mysql_error());
	$tot = mysql_num_rows($result1);
	while($posts = mysql_fetch_array($result1)){
		$post = $posts['Body'];
		$id = mysql_escape_string($posts['Id']);
		//$topic = $posts['Topic'];
		//echo $post.'<br>';
		$t = $posts['Topic'];
		$matches=0;
		foreach($concepts as $c){
			if(substr_count($post, $c)) {
			$d = mysql_escape_string($c); 
			$query2 = "insert into matches value('$id','$t','$d')";
			mysql_query($query2) or die(mysql_error());
			
				$matches++;
				if(isset($matched[$c]))
					$matched[$c] = $matched[$c]+1;
				else
					$matched[$c] = 1;
					
				
				//$topic[$t]++;
			}
		}
		if($matches>0) $v++;
	}
$tot_concept = count($matched);
echo '\nTotal Stackoverflow Posts with atleast 1 match '.$v.' out of '.$tot.'<br>';

foreach($matched as $a=>$b){
	echo '<tr><td>'.$a.'</td><td> '.$b.'</td></tr>';
}

?>